package cn.wolfcode.rbac.web.controller.system_manage;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.system_manage.IPermissionService;
import cn.wolfcode.rbac.service.system_manage.IRoleService;
import cn.wolfcode.rbac.util.PageResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @version 1.0
 * @Author:Cao Zhijianu0:05:37
 */
@Controller
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private IPermissionService iPermissionService;

    /**
     * 角色列表的显示
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @param model :
     * @return : java.lang.String
     */
    @RequestMapping("/role/list")
    @RequiredPermission({"角色列表","department:list"})
    public String list(Model model, QueryObject queryObject){

        // 查询出数据
        PageResult result = iRoleService.query(queryObject);
//        System.out.println(roles);
        // 回显数据
        model.addAttribute("result",result);

        return "/role/list";
    }

    /**
     * 删除角色
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @return : java.lang.String
     */
    @RequestMapping("/role/delete")
    @RequiredPermission({"角色删除","department:delete"})
    public String delete(Long id){
        iRoleService.delete(id);

        // 重定向列表界面
        return "redirect:/role/list";
    }

    /**
     * 新增和编辑角色跳转页面
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:14
     * @param id : 角色id
     * @return : java.lang.String
     */
    @RequestMapping("/role/input")
    public String input(Model model, Long id){

        if(id != null){
            // 如果id不为空，则为编辑功能，编辑功能要回显数据
            // 根据角色id查找角色信息
            Role role = iRoleService.get(id);
            model.addAttribute("entity",role);
        }

        model.addAttribute("permissions",iPermissionService.listAll());

        // 跳转到编辑界面
        return "/role/input";
    }

    /**
     * 新增和编辑角色保存数据库
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:27
     * @param role :
     * @return : java.lang.String
     */
    @RequestMapping("/role/saveOrUpdate")
    @RequiredPermission({"角色新增或编辑","department:saveOrUpdate"})
    public String saveOrUpdate(Role role,Long[] ids){

        iRoleService.saveOrUpdate(role,ids);

        return "redirect:/role/list";
    }
}
